package com.fingard.dsp.bank.directbank.cbhb01;

import java.math.BigDecimal;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CBHB01SinglePay extends CBHB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        String sendContent;
        if (p_req1908.singleDetail.sameBnk.equals("1")) {
            sendContent = d_1908_SameBank(p_req1908);
        } else {
            BigDecimal payAmount = new BigDecimal(p_req1908.singleDetail.payAmount);
            if (payAmount.compareTo(new BigDecimal(50000)) < 1) {
                sendContent = d_1908_DiffBankSmall(p_req1908);
            } else {
                sendContent = d_1908_DiffBankLarge(p_req1908);
            }
        }
        tmpRetDetail = d_190802_SglPay(p_req1908, sendContent);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    private String d_1908_SameBank(ReqSingle p_req1908) throws Exception {
        //赋值通用参数
        CBHB01Header bankHeader = new CBHB01Header();
        bankHeader.TransCode = "TXAC21";
        bankHeader.ClientSeqno = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        XElement tmpXElement = new XElement("body", "");
        tmpXElement.addChild(new XElement("PayAccNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("Ccy", convertCurToBank(p_req1908.singleDetail.payCur)));
        tmpXElement.addChild(new XElement("Amt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("TransUse", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("RecvAccName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("RecvAccNo", p_req1908.singleDetail.recAct));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_1908_DiffBankSmall(ReqSingle p_req1908) throws Exception {
        //赋值通用参数
        CBHB01Header bankHeader = new CBHB01Header();
        bankHeader.TransCode = "TXAC31";
        bankHeader.ClientSeqno = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        XElement tmpXElement = new XElement("body", "");
        tmpXElement.addChild(new XElement("PayAccName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("PayAccNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("Ccy", convertCurToBank(p_req1908.singleDetail.payCur)));
        tmpXElement.addChild(new XElement("Amt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("RecvAccName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("RecvAccNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("TransMemo", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("RecvBankNo", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("RecvBankName", p_req1908.singleDetail.cnapsName));
        tmpXElement.addChild(new XElement("BankNoFlag", "1"));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private String d_1908_DiffBankLarge(ReqSingle p_req1908) throws Exception {
        //赋值通用参数
        CBHB01Header bankHeader = new CBHB01Header();
        bankHeader.TransCode = "TXAC32";
        bankHeader.ClientSeqno = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        XElement tmpXElement = new XElement("body", "");
        tmpXElement.addChild(new XElement("TransUrgency", p_req1908.singleDetail.paySpd.equals("1") ? "1" : "0"));
        tmpXElement.addChild(new XElement("PayAccName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("PayAccNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("Ccy", convertCurToBank(p_req1908.singleDetail.payCur)));
        tmpXElement.addChild(new XElement("Amt", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("TransMemo", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("RecvAccName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("RecvAccNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("RecvBankNo", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("RecvBankName", p_req1908.singleDetail.cnapsName));
        tmpXElement.addChild(new XElement("BankNoFlag", "1"));
        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndDocument();
        return xmlWriter.toXmlString();
    }

    private RetAtsSglDetail d_190802_SglPay(ReqSingle p_req1908, String p_sendContent) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();

        String[] tmpStrRet = sendToBank(p_sendContent);
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/CBHB/head/RespondCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/CBHB/head/RespondInfo");
                String bkClientSeqno = tmpBkReader.getFirstText("/CBHB/head/ClientSeqno");
                if ("000000".equals(tmpRetCode) || "E00003".equals(tmpRetCode) || "650045".equals(tmpRetCode)) {
                    if (tmpBkReader.read("/CBHB/body")) {
                    	if(p_req1908.singleDetail.askNumber.equals(bkClientSeqno)){
                        	retDetail.respCode = "0";
                            retDetail.respInfo = tmpRetInfo;
                            retDetail.reqSeqID = bkClientSeqno;
                            retDetail.transState = tmpBkReader.readedXElement.getSingleText("TransStatus");
                            retDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("TransStatus");
                            retDetail.transAmt = tmpBkReader.readedXElement.getSingleText("Amt");
                        }else{
                        	retDetail.respCode = "-1";
                        	retDetail.respInfo = "响应流水号"+bkClientSeqno+"与请求"+p_req1908.singleDetail.askNumber+"不符";
                        }
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRetInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }
        return retDetail;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        CBHB01Header bankHeader = new CBHB01Header();
        bankHeader.TransCode = "QRTXST";

        bankHeader.ClientSeqno = getQryTransID();
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartElement("body");
        xmlWriter.writeElementClosed("QryBankSeqno");
        xmlWriter.writeElementString("QryClientSeqno", p_req1944.askNumber);
        xmlWriter.writeElementClosed("AcctNo");
        xmlWriter.writeElementClosed("BeginDate");
        xmlWriter.writeElementClosed("EndDate");
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/CBHB/head/RespondCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/CBHB/head/RespondInfo");
                if (tmpRetCode != null && tmpRetCode.equals("000000")) {
                    if (tmpBkReader.read("/CBHB/body/TransList/Record")) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpRetInfo;
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.transState = tmpBkReader.readedXElement.getSingleText("TransStatus");
                        retDetail.payInfoCode = tmpBkReader.readedXElement.getSingleText("TransStatus");
                        retDetail.transAmt = tmpBkReader.readedXElement.getSingleText("Amt");
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRetInfo;
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1944.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
